package com.mediacenter.widget;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RelativeLayout;

import com.mediacenter.R;

public class NDHorizontalScrollView extends HorizontalScrollView {
	//Context
	private Context mContext = null;
	//Resources
	private Resources mResources = null;
	//LayoutInflater
	private LayoutInflater mInflater = null;
	//Activity
	private Activity mActivity = null;
	//最外层RelativeLayout(该Layout包含Item和选中状态的图片ImageView)
	private	RelativeLayout scrollViewLayout = null;
	//Item组View  RadioGroup
	private RadioGroup itemRadioGroupView = null;
	//选中高亮状态View
	private ImageView highlightImageView = null;
	//左侧存在隐藏Item提示图标View
	private ImageView mLeftPromptImageView;
	//右侧存在隐藏Item提示图标View
	private ImageView mRightPromptImageView;
	//当前的RadioButton距离左侧的距离
	private int mCurrentCheckedRadioLeft;
	//RadioButton宽度
	private int radioBtnWidth = 0;
	//RadioButtong高度
	private int radioBtnHeight = 0;
	//屏幕宽度
	private int screenWitdh = 0;
	//RadioGroup焦点发生变化时的监听
	private OnCheckedChangeListener mRadioGroupcheckChangeListener = null;
	public NDHorizontalScrollView(Context context) {
		this(context, null);
	}
	public NDHorizontalScrollView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		mResources = mContext.getResources();
		mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		//获取RadioButton宽度和高度
		radioBtnWidth = mResources.getInteger(R.integer.function_menu_btn_width);
		radioBtnHeight = mResources.getInteger(R.integer.function_menu_btn_height);
		//初始化View
		initView();
		setBackgroundColor(0x00000000);
	}
	/**
	 * 初始化View
	 */
	private void initView() {
		//创建最外层RelativeLayout(该Layout包含Item和选中状态的图片ImageView)
		RelativeLayout scrollViewLayout = new RelativeLayout(mContext);
		scrollViewLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
		scrollViewLayout.setGravity(Gravity.TOP);//居中方式
		scrollViewLayout.setBackgroundColor(0x00000000);
		//创建Item组对象RadioGroup
		itemRadioGroupView = new RadioGroup(mContext);
		itemRadioGroupView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
		itemRadioGroupView.setOrientation(LinearLayout.HORIZONTAL);
		itemRadioGroupView.setBackgroundColor(0x00000000);
		itemRadioGroupView.setOnCheckedChangeListener(radioGrouponCheckedChangeListener);
		// android:layout_height="38dip"
        //android:layout_alignParentTop="true"
		//Item选中状态高亮效果ImageView
		highlightImageView = new ImageView(mContext);
		highlightImageView.setLayoutParams(new LayoutParams(radioBtnWidth, android.view.ViewGroup.LayoutParams.MATCH_PARENT));
		highlightImageView.setImageResource(R.drawable.bt_up_selector);
		highlightImageView.setScaleType(ScaleType.MATRIX);
		//android:layout_width="1dip"
        //android:layout_height="5dip"
        //android:layout_alignParentBottom="true"
		scrollViewLayout.addView(itemRadioGroupView);
//		scrollViewLayout.addView(highlightImageView);
		//添加到主View中
		addView(scrollViewLayout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
	}
	/**
	 * 设置左右二侧存在隐藏Item时提示View
	 * @param leftPromptImageView  左侧存在隐藏Item提示图标View
	 * @param rightPromptImageView 右侧存在隐藏Item提示图标View
	 */
	public void setPromptImageView(ImageView leftPromptImageView, ImageView rightPromptImageView) {
		//左侧存在隐藏Item提示图标View
		mLeftPromptImageView = leftPromptImageView;
		//右侧存在隐藏Item提示图标View
		mRightPromptImageView = rightPromptImageView;
	}
	/**
	 * 设置Activity类对象,并获取屏幕宽度.
	 * @param activity Actvity对象
	 */
	public void setActivity(Activity activity) {
		//Activity对象
		this.mActivity = activity;
		//获取屏幕宽度
		DisplayMetrics dm = new DisplayMetrics();
		mActivity.getWindowManager().getDefaultDisplay().getMetrics(dm);
		screenWitdh = dm.widthPixels;
	}
	
	/**
	 * 设置高亮View宽度和高度(可以不设置)
	 * @param width 宽度
	 * @param height 高度
	 */
	public void setHighlightViewLayoutParams(Integer width, Integer height) {
		if (highlightImageView != null) {
			ViewGroup.LayoutParams layoutParams = highlightImageView.getLayoutParams();
			if (layoutParams != null) {
				if (width != null) {
					layoutParams.width = width;
				}
				if (height != null) {
					layoutParams.height = height;
				}
			}
		}
	}
	/**
	 * 设置子View到RadioGroup中
	 * @param radioButtonList 要添加的RadioButton集合
	 */
	public void setRadioGroupItemView(List<RadioButton> radioButtonList) {
		if (radioButtonList == null) {
			return;
		}
		for (RadioButton radioButton : radioButtonList) {
			//添加到RadioGroup中.
			itemRadioGroupView.addView(radioButton);
		}
	}
	/**
	 * 添加子View到RadioGroup中
	 * @param itemValueList 要添加的数据(可以是字符串和Bitmap图片)
	 */
	@SuppressWarnings("deprecation")
	public void setRadioGroupItemValue(List<Object> itemValueList) {
		if (itemValueList == null || itemRadioGroupView == null) {
			return;
		}
		//删除所有的Item,重新添加.
		itemRadioGroupView.removeAllViews();
		//Item对象
		RadioButton itmeRadioButton = null;
		//Item Value
		Object itemValueObj = null;
		for (int index = 0; index < itemValueList.size(); index++) {
			itemValueObj = itemValueList.get(index);
			itmeRadioButton = (RadioButton)mInflater.inflate(R.layout.homepage_tabgroup_item, null);
			itmeRadioButton.setId(index);
			if (itemValueObj instanceof String) {
				//文字方式
				itmeRadioButton.setText(itemValueObj + "");
			} else if (itemValueObj instanceof Bitmap) {
				//图片方式
				itmeRadioButton.setButtonDrawable(new BitmapDrawable((Bitmap)itemValueObj));
			}
			itmeRadioButton.setLayoutParams(new LayoutParams(radioBtnWidth, radioBtnHeight));
			//设置焦点改变事件
			itmeRadioButton.setOnFocusChangeListener(new View.OnFocusChangeListener() {
				@Override
				public void onFocusChange(View view, boolean hasFocus) {
					if(hasFocus){
						view.performClick();
					}
				}
			});
			//添加到RadioGroup中.
			itemRadioGroupView.addView(itmeRadioButton, index);
		}
	}
	
	/**
	 * 设置选中指定索引RadioButton
	 * @param position 索引
	 */
	public void performClickRadioButton(int position){
		if (itemRadioGroupView != null && itemRadioGroupView.getChildAt(position) != null) {
			itemRadioGroupView.getChildAt(position).performClick();
		}
	}
	/**
	 * 显示和隐藏左右两边的箭头
	 */
	public void showAndHideArrow() {
		if (!mActivity.isFinishing() && scrollViewLayout != null) {
			this.measure(0, 0);
			if (screenWitdh >= this.getMeasuredWidth()) {
				if (mLeftPromptImageView != null) {
					mLeftPromptImageView.setVisibility(View.GONE);
				}
				if (mRightPromptImageView != null) {
					mRightPromptImageView.setVisibility(View.GONE);
				}
			} else {
				if (this.getLeft() == 0) {
					if (mLeftPromptImageView != null) {
						mLeftPromptImageView.setVisibility(View.GONE);
					}
					if (mRightPromptImageView != null) {
						mRightPromptImageView.setVisibility(View.VISIBLE);
					}
				} else if (this.getRight() == this.getMeasuredWidth() - screenWitdh) {
					if (mLeftPromptImageView != null) {
						mLeftPromptImageView.setVisibility(View.VISIBLE);
					}
					if (mRightPromptImageView != null) {
						mRightPromptImageView.setVisibility(View.GONE);
					}
				} else {
					if (mLeftPromptImageView != null) {
						mLeftPromptImageView.setVisibility(View.VISIBLE);
					}
					if (mRightPromptImageView != null) {
						mRightPromptImageView.setVisibility(View.VISIBLE);
					}
				}
			}
		}
	}
	protected void onScrollChanged(int l, int t, int oldl, int oldt) {
		super.onScrollChanged(l, t, oldl, oldt);
		if (!mActivity.isFinishing() && scrollViewLayout != null && mLeftPromptImageView != null && mRightPromptImageView != null) {
			if (scrollViewLayout.getWidth() <= screenWitdh) {
				mLeftPromptImageView.setVisibility(View.GONE);
				mRightPromptImageView.setVisibility(View.GONE);
			} else {
				if (l == 0) {
					mLeftPromptImageView.setVisibility(View.GONE);
					mRightPromptImageView.setVisibility(View.VISIBLE);
				} else if (scrollViewLayout.getWidth() - l == screenWitdh) {
					mLeftPromptImageView.setVisibility(View.VISIBLE);
					mRightPromptImageView.setVisibility(View.GONE);
				} else {
					mLeftPromptImageView.setVisibility(View.VISIBLE);
					mRightPromptImageView.setVisibility(View.VISIBLE);
				}
			}
		}
	}
	/**
	 * 为RadioGroup设置点击事件
	 * @param listener 事件
	 */
	public void setRadioGroupCheckedChangeListener(OnCheckedChangeListener listener) {
		mRadioGroupcheckChangeListener = listener;
	}
	/**
	 * RadioGroup点击CheckedChanged监听
	 */
	private OnCheckedChangeListener radioGrouponCheckedChangeListener = new OnCheckedChangeListener() {
		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			try {
				if (itemRadioGroupView != null && itemRadioGroupView.getChildCount() > 0 && itemRadioGroupView.getChildAt(checkedId) != null) {
					TranslateAnimation translateAnimation = new TranslateAnimation(mCurrentCheckedRadioLeft, ((RadioButton) itemRadioGroupView.getChildAt(checkedId)).getLeft(), 0f, 0f);
					translateAnimation.setInterpolator(new LinearInterpolator());
					translateAnimation.setDuration(100);
					translateAnimation.setFillAfter(true);
					highlightImageView.startAnimation(translateAnimation);
					//vPager.setCurrentItem(checkedId);// 让下方ViewPager跟随上面的HorizontalScrollView切换
					mCurrentCheckedRadioLeft = ((RadioButton) itemRadioGroupView.getChildAt(checkedId)).getLeft();
					smoothScrollTo((checkedId > 1 ? ((RadioButton) itemRadioGroupView.getChildAt(checkedId)).getLeft() : 0) - ((RadioButton) itemRadioGroupView.getChildAt(2)).getLeft(), 0);
				}
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			if (mRadioGroupcheckChangeListener != null) {
				mRadioGroupcheckChangeListener.onCheckedChanged(group, checkedId);
			}
		}
	};
	
	public void setRadioGrouponCheckedChangeListener(OnCheckedChangeListener radioGrouponCheckedChangeListener) {
		this.radioGrouponCheckedChangeListener = radioGrouponCheckedChangeListener;
	}
}
